package com.xiaoming;

import com.xiaoming.utils.StaticResourceUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * server.xml解析器
 */
public class ServerResolver {

    /***
     * 加载解析 web.xml 初始化 Servlet
     */
    public static void loadWebXml(String appBase, Map<String, HttpServlet> servletMap) throws Exception {
        FileInputStream fileInputStream = new FileInputStream(appBase + "\\web.xml");

        SAXReader reader = new SAXReader();
        Document document = reader.read(fileInputStream);
        Element rootElement = document.getRootElement();

        List<Element> servletClassList = rootElement.selectNodes("//servlet");
        List<Element> mappings = rootElement.selectNodes("//servlet-mapping");

        for (Element servletClass : servletClassList) {
            // 获取servlet
            Node node = servletClass.selectSingleNode("servlet-name");
            String servletName = node.getStringValue();
            Element element = (Element) servletClass.selectSingleNode("servlet-class");
            String servletClassName = element.getStringValue();
            // 获取servlet处理的请求映射
            Node servletMapping = rootElement.selectSingleNode("/web-app/servlet-mapping[servlet-name='" + servletName + "']");
            String urlPattern = servletMapping.selectSingleNode("url-pattern").getStringValue();
            LinClassLoader linClassLoader = new LinClassLoader(appBase);
            Class<?> aClass = linClassLoader.findClass(servletClassName);
            int start = appBase.lastIndexOf("\\");
            String projectName = appBase.substring(start + 1);
            HttpServlet servlet = (HttpServlet) aClass.getDeclaredConstructor().newInstance();
            servletMap.put("/" + projectName + urlPattern, servlet);
        }
    }

    public Map<String, HttpServlet> loadServer() throws DocumentException {
        InputStream webXmlStream = this.getClass().getClassLoader().getResourceAsStream("server.xml");
        SAXReader webXmlReader = new SAXReader();
        Document webXMl = webXmlReader.read(webXmlStream);
        Element server = webXMl.getRootElement();
        List<Element> hosts = server.selectNodes("//Host");
        Map<String, HttpServlet> servletMap = new HashMap<>();

        for (Element host : hosts) {
            String hostName = host.attributeValue("host");
            String path = host.attributeValue("appBase");
            StaticResourceUtil.path = path;
            File file = new File(path);
            File[] files = file.listFiles();
            for (File app : files) {
                if (app.isDirectory()) {
                    try {
                        loadWebXml(app.getPath(), servletMap);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

        }
        return servletMap;
    }
}
